﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Windows.Forms;
using SWEN_HMS.Classes;
using System.Net;
using System.Net.Mail;

namespace SWEN_HMS.BookingModule
{
    public partial class RoomBooking : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    ArrayList year = new ArrayList();

                    for (int i = 13; i < 29; i++)
                    {
                        year.Add(i);
                    }

                    DYear.DataSource = year;
                    DYear.DataBind();

                    ArrayList month = new ArrayList();

                    for (int i = 1; i < 13; i++)
                    {
                        month.Add(i);
                    }

                    DMonth.DataSource = month;
                    DMonth.DataBind();

                    cct.Visible = false;
                    creditcard.Enabled = false;
                    cardholdername.Enabled = false;
                    cvc.Enabled = false;
                    cardholderno.Enabled = false;
                    DMonth.Enabled = false;
                    DYear.Enabled = false;
                    cardHolderVali.Enabled = false;
                    ccNoVali.Enabled = false;
                    cvcVali.Enabled = false;
                }
            }
        }

        protected void paymentTypeDD_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (paymentTypeDD.SelectedItem.Text == "Credit Card")
            {
                creditcard.Enabled = true;

                cardholdername.Enabled = true;

                cvc.Enabled = true;

                cardholderno.Enabled = true;

                DMonth.Enabled = true;
                DYear.Enabled = true;

                cardHolderVali.Enabled = true;
                ccNoVali.Enabled = true;
                cvcVali.Enabled = true;
            }
            else if (paymentTypeDD.SelectedItem.Text == "Cash")
            {
                cct.Visible = false;

                creditcard.Enabled = false;

                cardholdername.Enabled = false;

                cvc.Enabled = false;

                cardholderno.Enabled = false;

                DMonth.Enabled = false;
                DYear.Enabled = false;

                cardHolderVali.Enabled = false;
                ccNoVali.Enabled = false;
                cvcVali.Enabled = false;
            }
            else
            {
                cct.Visible = false;

                creditcard.Enabled = false;

                cardholdername.Enabled = false;

                cvc.Enabled = false;

                cardholderno.Enabled = false;

                DMonth.Enabled = false;
                DYear.Enabled = false;

                cardHolderVali.Enabled = false;
                ccNoVali.Enabled = false;
                cvcVali.Enabled = false;
            }
        }

        protected void creditcard_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (creditcard.SelectedItem.Text == "Please Select Card Type")
            {
                cct.Visible = true;
                cct.Text = "Please select card type";
                cardholdername.Enabled = false;
                cvc.Enabled = false;
                cardholderno.Enabled = false;
                DMonth.Enabled = false;
                DYear.Enabled = false;
                cardHolderVali.Enabled = false;
                ccNoVali.Enabled = false;
                cvcVali.Enabled = false;
            }
            else
            {
                cct.Visible = false;
                cardholdername.Enabled = true;
                cvc.Enabled = true;
                cardholderno.Enabled = true;
                DMonth.Enabled = true;
                DYear.Enabled = true;
                cardHolderVali.Enabled = true;
                ccNoVali.Enabled = true;
                cvcVali.Enabled = true;
            }
        }

        protected void countCOD_Click(object sender, EventArgs e)
        {
            double day = double.Parse(durationOfStay.Text);
            DateTime checkin = Calendar1.SelectedDate.AddDays(day-1);
            checkOutDate.Visible = true;
            checkOutDate.Text = checkin.ToString("dd/MM/yyyy");
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            checkInDate.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy");
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            DateTime pastday = e.Day.Date;
            DateTime date = DateTime.Now;
            int year = date.Year;
            int month = date.Month;
            int day = date.Day;
            DateTime today = new DateTime(year, month, day);
            if (pastday.CompareTo(today) < 0)
            {
                e.Cell.BackColor = System.Drawing.Color.Gray;
                e.Day.IsSelectable = false;
            }
        }

        protected void booking_Click(object sender, EventArgs e)
        {
            //for booking of room 
            Booking book = new Booking();
            book.firstName = TextBox1.Text;
            book.lastName = TextBox2.Text;
            book.contactNo = TextBox3.Text;
            book.address = TextBox5.Text;
            book.email = TextBox4.Text;
            book.noOfAdult = Convert.ToInt32(TextBox10.Text);
            book.noOfChild = Convert.ToInt32(TextBox11.Text);
            book.roomNo = Convert.ToInt32(roomNoSelection.SelectedValue);
            book.roomType = roomType.SelectedValue;
            book.paymentType = paymentTypeDD.SelectedValue;
            book.cardholderName = cardholdername.Text;

            if (creditcard.Enabled == false)
            {
                book.cardType = "null";
            }
            else
            {
                book.cardType = creditcard.SelectedValue;
            }

            if (cardholderno.Enabled == false)
            {
                book.cardNo = "null";
            }
            else
            {
                book.cardNo = cardholderno.Text;
            }

            if (DMonth.Enabled == false && DYear.Enabled == false)
            {
                book.expDate = DMonth.SelectedValue + "/" + DYear.SelectedValue;
            }
            else
            {
                book.expDate = DMonth.SelectedValue + "-" + DYear.SelectedValue;
            }

            if (cvc.Enabled == false)
            {
                book.cardCVC = "null";
            }
            else
            {
                book.cardCVC = cvc.Text;
            }

            book.checkIn = checkInDate.Text;
            book.checkOut = checkOutDate.Text;
            book.noOfDay = Convert.ToInt32(durationOfStay.Text);
            book.remarks = remarks.Text;
            book.staffID = Convert.ToInt32(Session["staffid"]);
            book.checkInStatus = "False";

            int row = RoomDBManager.createBooking(book);
            if (row > 0)
            {
                System.Windows.Forms.MessageBox.Show("Booking have been made.");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("There's is an error");
            }

            //for updating room

            Room rm = new Room();
            rm.roomNo = Convert.ToInt32(roomNoSelection.SelectedValue);
            rm.roomStatus = "Occupied";
            rm.noOfAdult = Convert.ToInt32(TextBox10.Text);
            rm.noOfChild = Convert.ToInt32(TextBox11.Text);
            RoomDBManager.updateRoomStatus(rm);

            //add subscription
            if (Subscription.Checked == true)
            {
                Subscription check = RoomDBManager.searchForSubscription(TextBox4.Text);
                if (check.email == TextBox4.Text)
                {
                    System.Windows.Forms.MessageBox.Show("The guest have already made a subscription");
                }
                else
                {
                    Subscription s = new Subscription();
                    s.firstName = TextBox1.Text;
                    s.lastName = TextBox2.Text;
                    s.email = TextBox4.Text;
                    RoomDBManager.addSubscription(s);
                }
            }
            else
            {
                try
                {
                    EncodingAndDecodingcs de = new EncodingAndDecodingcs();
                    MailMessage mail = new MailMessage();
                    mail.From = new MailAddress("swenhms@gmail.com");
                    mail.To.Add(TextBox4.Text);
                    mail.Subject = "Booking Information";
                    mail.Body = "Dear " + TextBox1.Text + TextBox2.Text + "," + Environment.NewLine + Environment.NewLine +
                        "Thank You for booking with Delonix Regia Hotel," + Environment.NewLine + "Below is the booking information that you have made and the room that we have assign to you." + Environment.NewLine + Environment.NewLine +
                        Environment.NewLine + "Room Number:" + roomNoSelection.SelectedValue + Environment.NewLine + "Room Type:" + roomType.SelectedValue + Environment.NewLine + "Check In Date:" + checkInDate.Text + Environment.NewLine +
                        "Check-Out Date:" + checkOutDate.Text + Environment.NewLine + "Number of Days:" + durationOfStay.Text + Environment.NewLine + Environment.NewLine + "This is a generated E-mail. Please do not reply" + Environment.NewLine + Environment.NewLine + "Best Regards" + Environment.NewLine + " Delonix Regia Team";

                    SmtpClient smtp = new SmtpClient("smtp.gmail.com");
                    smtp.Credentials = new NetworkCredential("swenhms@gmail.com", "HMS12345");
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    smtp.Send(mail);
                }
                catch
                {
                    throw;
                }
            } Response.Redirect("~/BookingModule/RoomAvailability.aspx");
            
        }

        protected void roomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ArrayList roomNo = RoomDBManager.getDetailByRoomType(roomType.SelectedValue);
            roomNoSelection.DataSource = roomNo;
            roomNoSelection.DataBind();
        }


        public void clear()
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";

            roomType.SelectedValue = null;
            roomNoSelection.SelectedValue = null;
            TextBox10.Text = "";
            TextBox11.Text = "";

            checkOutDate.Text = "";
            checkOutDate.Visible = false;
            checkInDate.Text = "";
            durationOfStay.Text = "";

            paymentTypeDD.SelectedValue = null;
            creditcard.SelectedValue = null;
            creditcard.Enabled = false;

            cardholdername.Text = "";
            cardholdername.Enabled = false;

            cardholderno.Text = "";
            cardholderno.Enabled = false;

            cvc.Text = "";
            cvc.Enabled = false;

            DMonth.SelectedValue = null;
            DMonth.Enabled = false;
            DYear.SelectedValue = null;
            DYear.Enabled = false;

            remarks.Text = "";
            Subscription.Checked = false;       
        }

        protected void cancel_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";

            roomType.SelectedValue = null;
            roomNoSelection.SelectedValue = null;
            TextBox10.Text = "";
            TextBox11.Text = "";

            checkOutDate.Text = "";
            checkOutDate.Visible = false;
            checkInDate.Text = "";
            durationOfStay.Text = "";
            
            paymentTypeDD.SelectedValue = null;         
            creditcard.SelectedValue = null;
            creditcard.Enabled = false;

            cardholdername.Text = "";
            cardholdername.Enabled = false;

            cardholderno.Text = "";
            cardholderno.Enabled = false;

            cvc.Text = "";
            cvc.Enabled = false;

            DMonth.SelectedValue = null;
            DMonth.Enabled = false;
            DYear.SelectedValue = null;
            DYear.Enabled = false;

            remarks.Text = ""; 
            Subscription.Checked = false;             
        }
    }
}